import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PostsModule } from './posts/posts.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigService, ConfigModule } from '@nestjs/config';
import envConfig from '../config/env';
import { PostsEntity } from './posts/posts.entity';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true, // 全局生效
      envFilePath: [envConfig.path],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        type: 'mysql', //数据库类型
        entities: [PostsEntity], //实体类
        host: configService.get('DB_HOST', 'localhost'), //数据库地址
        port: configService.get<number>('DB_PORT', 3306), //端口
        username: configService.get('DB_USER', 'root'), //用户名
        password: configService.get('DB_PASSWORD', 'root1234'), //密码
        database: configService.get('DB_DATABASE', 'blog'), //数据库名称
        timezone: '+08:00', //服务器上配置的时区
        synchronize: false, //根据实体自动创建数据库表,生产环境建议关闭
      }),
    }),
    PostsModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
